<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
$return['mid']=$_SESSION['uid'];
$return['show_setmobile']=cfc('core')->check_setmobile(1);
$return['show_setmember']=cfc('core')->check_setmember(1);
if($_RQ['op']=='main'){
	//去除匿名下单功能
	if($_RQ['goodsid']){
		$login_burl='jy_duotuan/goods.detail//id='.$_RQ['goodsid'];
		$this->session('login_burl',$login_burl);
	}
	$return=model('jy_weishop/get_order')->getdata($_RQ);
	if($_SESSION['openid'.$merchid] || $_SESSION['onemerchid']){
		$return['onemerchid']=$_SESSION['onemerchid']?:$merchid;
	}
	foreach($return['order']['list'] as $item){
		$item['api_data']=json_decode($item['api_data'],true);
		if($item['api_data']['dingpei']){
			$return['post']['is_dingpei']=1;
			break;
		}
	}
	$return['sendtypes']=array(
		'1'=>'快递配送',
		'2'=>'上门自提',
	);
	if($return['sendtypes'][1]){
		$return['sendtypes'][1]=$this->send_types[1];
	}
	if($return['sendtypes'][2]){
		$return['sendtypes'][2]=$this->send_types[2];
	}
	if($this->setting['no_sendtype1'] && $return['sendtypes'][1]){
		unset($return['sendtypes'][1]);
	}
	$return['post']['sendtype']=2;
	if($this->plugin_exists('jy_area_dian')){
		$area_dian=model('jy_area_dian/member')->mydian();
		if($area_dian){
			$area_dian['pcd']=json_decode($area_dian['pcd'],true);
			$return['area_dians'][]=$area_dian;
			$return['area_dian']=$area_dian;
			$return['post']['area_dianid']=$area_dian['id'];
		}
	}
	$return['member']['credit5']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'credit5');
	//返回链接
	$return['forward_url']=base64_encode('jy_duotuan/order.list//fromcreate=1');
	$return['pluginset']=$this->setting;
	set_html('title','确认订单');
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$topaytype=$post['topaytype'];
	//整理订单数据
	$new_order=model('jy_weishop/get_order')->new_order($post);
	$new_order['plugin_name']='jy_duotuan';
	if(!$new_order['merchid'] && $_SESSION['onemerchid']){
		$new_order['api_data']=array('dhmerchid'=>$_SESSION['onemerchid']);
	}
	if($post['area_dianid']){
		$new_order['api_data']['area_dianid']=$post['area_dianid'];
	}
	if(!$post['area_dianid'] && $post['send_type']==2){
		exi('请选择自提点！','error');
	}
	$basic=$this->plugin_setting('basic');
	if($topaytype){
		$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit5','credit6','mobile','avatar','nickname'));
		$basic['bei_perbuy']=$basic['bei_perbuy']?:70;
		$basic['bei_feeorder']=$basic['bei_feeorder']?:100;
		$basic['bei_perorder']=$basic['bei_perorder']?:16;
		$basic['bei_buorder']=$basic['bei_buorder']?:16;
		$needcredit5=$member['credit5']*$basic['bei_perbuy']/100;
		if($needcredit5<$new_order['price']){
			exi('当前备用金不足！不能下单！','error');
		}
		//如够则计算（备用金-商品金额=是否大于N并且大于商品金额*16%)如大于，则+1扣备用金
		$perorderfee=$basic['bei_perorder']*$new_order['price']/100;
		if($member['credit5']-$new_order['price']>$basic['bei_feeorder'] && $member['credit5']-$new_order['price']>$perorderfee){
		}else{
			$topaytype=0;
			$payfee=$new_order['price']*$basic['bei_buorder']/100;
		}
		$credit5fee=$new_order['price']*100/$basic['bei_perbuy'];
		$credit5fee=$credit5fee+1;
	}
	$order_id=model('jy_weishop/order')->create($new_order);
	$success_url='jy_duotuan/order.list/';
	if($topaytype){
		cfc('core')->member_credit($_SESSION['uid'],'credit5',$credit5fee,false,'jy_duotuan','备用金下单');
		$params=array(
			'paytype'=>'credit5',
			'creditfee'=>$credit5fee,
		);
		model('jy_weishop/order')->order_pay($order_id,$params);
	}else{
		$params=array(
			'id'=>$order_id,
			'title'=>'购买商品',
			'fee'=>$payfee?:$new_order['price'],
			'oldfee'=>$new_order['oldfee'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_duotuan/order.create/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_duotuan/order.list/',
			'error_url'=>$error_url?:'jy_duotuan/order.list/',
		);
		//虚拟设置
		$plugin_xuni=model('jy_weishop/activity')->plugin_xuni($new_order);
		if(count($plugin_xuni)){
			$params['plugin_xuni']=$plugin_xuni;
		}
		$payid=cfc('pay')->create_pay_order($params);
		$update=array('pay_id'=>$payid);
		if($credit5fee>0){
			$update['creditfee']=$credit5fee;
		}
		pdo_update('jy_weishop_order',$update,array('id'=>$order_id));
		if($post['peerpay']){
			$peerpay_setting=plugin('jy_weishop')->plugin_setting('peerpay');
			if($peerpay_setting && $peerpay_setting['open']){
				exi('','','jy_weishop/order.peerpay//orderid='.$order_id);
			}
		}
	}
	$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>$success_url,
			'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_duotuan/order.list/');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_duotuan/order.list/');
	}
	if($order['price']!=$payresult['fee'] && !$order['creditfee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_duotuan/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	if($order['creditfee']>0){
		cfc('core')->member_credit($_SESSION['uid'],'credit5',$order['creditfee'],false,'jy_duotuan','备用金下单');
		$params=array(
			'paytype'=>'credit5',
		);
	}
	model('jy_weishop/order')->order_pay($order['id'],$params);
	$order['api_data']=json_decode($order['api_data'],true);
	if($this->plugin_exists('jy_area_dian') && $order['api_data']['area_dianid']){
		model('jy_area_dian/daishou')->create_order($order,$order['api_data']['area_dianid']);
	}
	exi('提交成功!');
}

?>